/* Read/Write binary data from/into file */

/* Copyright (c) 2007-2012, ben "boin" bonoeil

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the <organization> nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

    NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
    GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY
    ben "boin" bonoeil ''AS IS'' AND ANY EXPRESS OR IMPLIED
    WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL ben "boin" bonoeil BE LIABLE FOR ANY
    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
    GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "hl2demo_io.h"
#include "hl2demo_ieee754.h"

#include <stdint.h>

int read_byte(FILE * inp, void * b) {
  int err =
    b
    ? -(1 != fread(b,1,1,inp))
    : fseek(inp,1,SEEK_CUR);
  return err;
}

int write_byte(FILE * inp, const void * b) {
  int err =
    b
    ? -(1 != fwrite(b,1,1,inp))
    : fseek(inp,1,SEEK_CUR);
  return err;
}

int read_int32(FILE * inp, int * i) {
  int err;

  if (i) {
    uint8_t j[4];
    err = -(4 != fread(j,1,4,inp));
    if (!err) {
      *i = (int)(int32_t)(j[0] | (j[1]<<8) | (j[2]<<16) | j[3]<<24);
    }
  } else {
    err = fseek(inp,4,SEEK_CUR);
  }
  return err;
}

int write_int32(FILE * inp, const int32_t * i) {
  int err;

  if (i) {
    const int v = *i;
    uint8_t j[4];
    j[0] = v;    j[1] = v>>8;    j[2] = v>>16;    j[3] = v>>24;
    err = -(4 != fwrite(j,1,4,inp));
  } else {
    err = fseek(inp,4,SEEK_CUR);
  }
  return err;
}


int read_float32(FILE * inp, float * i) {
  int err;

  if (!i) {
    err = fseek(inp,4,SEEK_CUR);
  } else {
    int32_t v;
    err = read_int32(inp, &v);
    if (!err) {
      *i = ieee754_get32(v);
    }
  }

  return err;
}

int write_float32(FILE * inp, const float * f) {
  int err;

  if (f) {
    uint8_t j[4];
    int32_t v;
    ieee754_set32(&v, *f);
    j[0] = v;    j[1] = v>>8;    j[2] = v>>16;    j[3] = v>>24;
    err = -(4 != fwrite(j,1,4,inp));
  } else {
    err = fseek(inp,4,SEEK_CUR);
  }
  return err;
}

int read_data(FILE * inp, void * buf, int len) {
  if (len) {
    if (buf) {
      len = - (len != fread(buf, 1, len, inp));
    } else {
      len = fseek(inp, len, SEEK_CUR);
    }
  }
  return len;
}

int write_data(FILE * inp, const void * buf, int len) {
  if (len) {
    if (buf) {
      len = - (len != fwrite(buf, 1, len, inp));
    } else {
      len = fseek(inp, len, SEEK_CUR);
    }
  }
  return len;
}
